/* (C) 2007-2009 caitong Limited
 * (C) 2009-2012  limited
 *
 * Authors:
 * legs.ant.king@gmail.com


   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
einclude <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <strstream>
#include "../include/unistd.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <signal.h>
//#include <windows.h>
#include "../include/Conn.h"
#include "../include/iGetOpt.h"
#include "../include/iInitPort.h"
#include "../include/iLoadCfg.h"
#include "../include/SentMsg.h"
#include "../include/InitVariable.h"
#include "../include/iGetCfg.h"
#include <math.h>
#include <cstdlib>

using namespace std;
char iArgument[][50]={"","","","","","","","","","","","","","","","", "", "", "", "", "", "", ""};
//0=user;1=password;2=hostname;3=port;4=socket;5=dbname;6=Resource sent sphone;7=phonepwd;8=sent to \
phone;9=config;10=max warning MaxWarningValue value;11=daemon mode;12=logfile;13=errsentperf;14=warsentperf;15=sleeptime;16=Sentcount;17;
char iCompasion[][50]={"user","password","hostname","port","socket","dbname","sphone","phonepwd","phone","Config",\
	                     "MaxWarningValue","daemon","logfile","errsentperf","warsentperf","sleeptime","sentcount",\
	                     "programsleep","sentmsg", "sendto", "email" ,"remail", "mailcount"};
						 //email send to emailbox .. rmail resouce send mail emal ... sendto whether send email ...
char iPortList[][10]={0};
void iInfo();
int   CreateDir(const   char   *sPathName);

int daemon_init() 
{ 
	pid_t pid; 
	if((pid = fork()) < 0)
		return(-1); 
	else if(pid != 0)
		exit(0); 
	setsid();
	chdir("/var/log");
	umask(0);
	close(0);
	close(1);
	close(2); 
	return(0);
} 

void sig_term(int signo) 
{ if(signo == SIGTERM)
	{
		syslog(LOG_INFO, "repmsg program terminated."); 
		closelog();
		exit(0);
	} 
} 

int main(int argc,char **argv)
{
	iInfo(); 
	GetCfg(argc,argv,iArgument,iCompasion);
	loadcfg(argc,argv,iArgument,iCompasion);
	getopts(argc,argv,iArgument,iCompasion);
	iInitPortValue(iPortList,iArgument,iCompasion);
	iLastInitVariable(iArgument,iCompasion);
	int Daemonid=GetMatachChar(iCompasion,"daemon");
	int iDaemonid=atoi(iArgument[Daemonid]);
	CreateDir("/var/logs/");
	if(iDaemonid==1)
	{
		cout<<"Running to Daemon Mode .\n";
		if(daemon_init() == -1) 
		{
			printf("can't fork self\n"); exit(0); 
		} 
		openlog("Repmsg", LOG_PID, LOG_USER); 
		syslog(LOG_INFO, "repmsg program started."); 
		signal(SIGTERM, sig_term);  
	} 

Conncect(iPortList,iArgument,iCompasion);
//	for(int i=0;i<ArgumentCount();i++)
//		cout<<iArgument[i]<<endl;
	//sendtomsg("hello world",iCompasion ,iArgument);
	//SentMsg("tengjh nin hao!" ,iCompasion,iArgument);

}

int ArgumentCount()
{
	return(sizeof(iCompasion)/sizeof(iCompasion[0]));
}

int   CreateDir(const   char   *sPathName)   
  {   
  char   DirName[256];   
  strcpy(DirName,   sPathName);   
  int   i,len   =   strlen(DirName);   
  if(DirName[len-1]!='/')   
  strcat(DirName,   "/");   
    
  len   =   strlen(DirName);   
    
  for(i=1;   i<len;   i++)   
  {   
  if(DirName[i]=='/')   
  {   
  DirName[i]   =   0;   
  if(   access(DirName,   NULL)!=0   )   
  {   
      if(mkdir(DirName,   0755)==-1)   
      {   
                      perror("mkdir   error");   
                      return   -1;   
      }   
  }   
  DirName[i]   =   '/';   
  }   
  }   
    
  return   0;   
  }   

